const cubePosition = [...window.numberPosition, ...window.letterPosition, ...window.specialCharacterPosition, ...window.iconPosition];
let string_position = [];
let ledCubeLamp = [];//当前led矩阵点亮的灯
const ccb = {
  delay: function(ms){{
    return new Promise(resolve =>{
      setTimeout(resolve, ms);
    })
  }},
  //显示图标
  muti_led_show_with_icon: async function(icon){
    closeWindowLed()
    get_icon_position(icon);
    await showAllString();
    await ccb.delay(500);
  },
  //显示字符串
  muti_led_show_with_str: async function(str){
    closeWindowLed();
    get_string_position(str);
    setStringPosition();
    await showAllString();
    await ccb.delay(500);
  },
  //显示自定义矩阵
  muti_led_show_with_but: async function(str){
    closeWindowLed();
    get_btn_position(str);
    await showAllString();
    await ccb.delay(500);
  },
  //点亮或者熄灭led矩阵的第index个灯
  muti_led_set_flag_with_no: async function(index, flag){
    get_index_position(index, flag);
    await showAllString();
    await ccb.delay(500);
  },
  muti_led_set_on_with_xy: async function(x, y){
    console.log(x, y);
    await ccb.delay(500);
  }
}

//发送led矩阵状态到react组件中
function sendLedStatusToReact(position){
  const led_data = compute_led_stataus(position);
  const data = {
    type: 'ccb_function',
    action_type: 'led',
    data: led_data
  }
  window.postMessage(data)
}
//获取图标位置
function get_icon_position(icon){
  string_position = [];
  let theIcon;
  for (let i = 0; i < cubePosition.length; i++) {
    if(icon == cubePosition[i].name){
      theIcon = cubePosition[i]
      break;
    }
  }
  if(theIcon){
    string_position[0] = theIcon;
  }
}

function get_x_y_position(x, y){
  position = [];
  for(let i = 0; i < ledCubeLamp.length; i++){
    position.push({
      x: x,
      y: y
    })
  }
  const ledData = {
    position: position
  }
  string_position[0] = ledData;
}

//获取点亮（熄灭）新的led矩阵位置信息
function get_index_position(index, flag){
  set_ledCubeLamp(index, flag);
  position = [];
  for(let i = 0; i < ledCubeLamp.length; i++){
    position.push(pointToPosition(ledCubeLamp[i]))
  }
  const ledData = {
    position: position
  }
  string_position[0] = ledData;
}
/**
 * [set_ledCubeLamp 设置ledCubeLamp状态]
 */
function set_ledCubeLamp(index, flag){
  const indexP = parseInt(index)
  if(flag){
    if(ledCubeLamp.indexOf(indexP) == -1){
      ledCubeLamp.push(indexP)
    }
  }else{
    ledCubeLamp = ledCubeLamp.filter((item)=>{
      return item != indexP
    })
  }
}
//获取自定义字符串坐标位置
function get_btn_position(str){
  string_position = []
  function show_icon(string_matrix){
    const matrixPosition = []
    Object.keys(string_matrix).forEach((k)=>{
      if(string_matrix[k] == '#'){
        const matrixItem = pointToPosition(k)
        matrixItem.status = true
        matrixPosition.push(matrixItem)
        // set_ledCubeLamp(k, true)
      }else{
        // set_ledCubeLamp(k, false)
      }
    })
    return matrixPosition
  }
  const ledArr = show_icon(str);
  const ledData = {
    position: ledArr
  }
  string_position[0] = ledData;
}
/**
 * [pointToPosition 点转化成坐标]
 * @param  {[type]} num [description]
 * @return {[type]}          [description]
 */
function pointToPosition(num) {
  const position = {
    x: num % 7,
    y: parseInt(num / 7)
  }
  return position
}
/**
 * [get_string_position 拼接字符串原始位置]
 * @param  {[type]} showString [description]
 * @return {[type]}            [description]
 */
function get_string_position(showString){
  string_position = []
  Object.keys(showString).forEach((k)=>{
    const str = showString[k]
    
    for(let i = 0; i < cubePosition.length; i++){
      if(str === ' '){
        const kongString = {
          name: ' ',
          type: 'string',
          position: []
        }
        string_position.push(kongString) 
        break;
      }else{
        if(cubePosition[i].name == str){
          const cubeRect = JSON.parse(JSON.stringify(cubePosition[i]))//深拷贝
          string_position.push(cubeRect) 
          break;
        }
      }
    }
  })
}
/**
 * [setStringPosition 排列显示数字的位置]
 * @param {[type]} string_position [数组]
 */
function setStringPosition(){
  for(let i = 0; i < string_position.length; i++){
    for(let j = 0; j < string_position[i].position.length; j++){
      string_position[i].position[j].x = string_position[i].position[j].x + i * 7
    }
  }
}
/**
 * [showStringPosition 显示灯亮]
 * @param  {[type]} string_position [description]
 * @return {[type]}                     [description]
 */
function showStringPosition(){
  let positions = [];
  for(let i = 0; i < string_position.length; i++){
    positions = positions.concat(string_position[i].position);
  }
  sendLedStatusToReact(positions)
}
/**
 * [showAllString 展示所有字符串，大于1个长度滚动]
 * @param  {[type]}  [description]
 * @return {[type]}                 [description]
 */
async function showAllString(){
  if(string_position.length > 1){
    sendLedStatusToReact(string_position[0].position)
    for(let i = 0;  i < (string_position.length - 1) * 7; i++ ){
      await ccb.delay(200);
      closeWindowLed();
      stringPositionLeft();
      showStringPosition();
    }
  }else if(string_position.length === 1){
    sendLedStatusToReact(string_position[0].position)
  }else {
    console.log('字符串不存在');
  }
}
/**
 * 字符串左移
 */
function stringPositionLeft(){
  for(let i = 0; i < string_position.length; i++){
    for(let j = 0; j < string_position[i].position.length; j++){
      string_position[i].position[j].x = string_position[i].position[j].x - 1
    }
  }
}
/**
 * 熄灭led矩阵
 */
function closeWindowLed(){
  const temporary_led_highlight = JSON.parse(JSON.stringify(led_highlight_origin));
    const data = {
      type: 'ccb_function',
      action_type: 'led',
      data: temporary_led_highlight
    }
    window.postMessage(data)
}
//计算当前led高亮
function compute_led_stataus(the_position){
  const temporary_led_highlight = JSON.parse(JSON.stringify(led_highlight_origin));
  const the_position_index = [];
  for (let i = 0; i < the_position.length; i++) {
    if(the_position[i].x >= 0 && the_position[i].x <= 6){//字符串会向左移动
      the_position_index.push(the_position[i].x + the_position[i].y * 7);
    }
  }

  for (let i = 0; i < Object.keys(temporary_led_highlight).length; i++) {
    if (the_position_index.indexOf(i) > -1) {
      temporary_led_highlight["led_" + i] = true;
    } else {
      temporary_led_highlight["led_" + i] = false;
    }
  }
  return temporary_led_highlight;
  // const the_position = cubePosition.find((item) => {
  //   return item.name == current_letter;
  // }).position;
  // const temporary_led_highlight = JSON.parse(JSON.stringify(led_highlight_origin));
  // const the_position_index = [];
  // for (let i = 0; i < the_position.length; i++) {
  //   the_position_index.push(the_position[i].x + the_position[i].y * 7);
  // }

  // for (let i = 0; i < Object.keys(temporary_led_highlight).length; i++) {
  //   if (the_position_index.indexOf(i) > -1) {
  //     temporary_led_highlight["led_" + i] = true;
  //   } else {
  //     temporary_led_highlight["led_" + i] = false;
  //   }
  // }
  // return temporary_led_highlight;
}